package com.ihr360.payroll.model.dto.salary.embeddable;


import com.ihr360.commons.model.type.FieldSource;
import com.ihr360.payroll.config.FieldSourcesEntity;
import com.ihr360.payroll.model.dto.salary.superclass.AbstractSalaryFieldValue;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import java.util.Arrays;

/**
 * 薪资字段的字段取值配置
 */
@Embeddable
@TypeDefs({
        @TypeDef(name = "json", typeClass = JsonStringType.class),
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
public class SystemSalaryFieldValue
        extends AbstractSalaryFieldValue<SystemSalaryFieldValue> {
    private static final long serialVersionUID = -4887088470519672344L;

    /**
     * 字段支持的取值方式列表
     */
    @Column(columnDefinition = "text")
    @Type(type = "json")
    protected FieldSourcesEntity sources;

    /**
     * 默认字段取值方式
     */
    @Enumerated(EnumType.STRING)
    protected FieldSource defaultSource = FieldSource.INPUT;


    public FieldSourcesEntity getSources() {
        if (sources == null) {
            FieldSourcesEntity sourcesEntity = new FieldSourcesEntity();
            sourcesEntity.setData(Arrays.asList(defaultSource));
            return sourcesEntity;
        }
        return sources;
    }

    public void setSources(FieldSourcesEntity sources) {
        this.sources = sources;
    }

    public FieldSource getDefaultSource() {
        return defaultSource;
    }

    public void setDefaultSource(FieldSource defaultSource) {
        this.defaultSource = defaultSource;
    }
}
